package com.yonyou.rabbitmq.provider.basic.woker;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.yonyou.rabbitmq.utils.RabbitmqUtils;

import java.io.IOException;

public class Provider {

    public static void main(String[] args) {

        Connection connection = null;
        Channel channel = null;

        try {
            //获取连接对象
            connection = RabbitmqUtils.getConnection();
            if (connection != null) {
                //获取通道对象
                channel = connection.createChannel();
                //声明队列
                channel.queueDeclare("work",true,false,false,null);

                //生产消息
                String message = "";
                for (int i = 1; i <= 20; i++) {
                    message = "work queues,id:"+i;
                    channel.basicPublish("","work",null,message.getBytes());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("生产者发布消息完成......");
            RabbitmqUtils.close(channel,connection);
        }
    }

}
